function toHtmlData(target,title, showHeader,isRowspan ){ 
	var dg = $(target);
	var table = {};
	table.data = dg.datagrid('toArray');
	table.title = title;
	table.cols = table.data[0].length ; 
	table.showHeader = showHeader  ;
    table.isRowspan = isRowspan == null || isRowspan  == undefined ? false : isRowspan ;

    var fields = dg.datagrid('getColumnFields',true).concat(dg.datagrid('getColumnFields',false));
    var data = [];
    table.fields = [];
    for(var i=0; i<fields.length; i++){
        var col = dg.datagrid('getColumnOption', fields[i]);
        table.fields.push({title:col.title,width:col.width,colspan:col.colspan,rowspan:col.rowspan });
    }
	return table;
}

function toHtml (datas ){

       var data = [];

	 	var trStyle = 'height:32px';
        var tdStyle0 = 'vertical-align:middle;padding:0 4px';

        for(var i = 0 ; i < datas.length ; i ++){//表
        	var t = datas[i];
            data.push('<table border="1" rull="all" style="border-collapse:collapse">');
        	///标题
        	if(t.title != null && t.title != undefined && t.title.trim() != ""){
        		data.push('<tr style="'+trStyle+'">');
        		data.push('<td colspan="'+t.cols+'" style="'+tdStyle0+';text-align:center">'+t.title+'</td>');
        		data.push('</tr>');
        	}
        	if(t.showHeader == undefined) t.showHeader = true;
        	//行
        	for(var j = 0 ; j <t.data.length ; j ++){
        		if(t.showHeader == false && j == 0 ) continue;
        		var trd = t.data[j];
        		data.push('<tr style="'+trStyle+'">');

        		for(var k =0 ; k <trd.length ; k ++){

        			var field = t.fields[k];

        			if(field == undefined ){
        				field ={width:100,colspan:1};
        			}
					var tdStyle = tdStyle0 + ';width:'+field.width+'px;';

 					data.push('<td colspan="'+field.colspan+'" '+(t.isRowspan ?'rowspan="'+field.rowspan+'"' : "") +'  style="'+tdStyle+'">'+(trd[k]== null ||trd == undefined ? '':trd[k] ) +'</td>');

 				}
            
                data.push('</tr>');
        	}
            data.push('</table>');


        }
        return data.join('');

}

 function toExcel(table, param){
        var filename = null;
        var rows = null;
        var worksheet = 'Worksheet';
        if (typeof param == 'string'){
            filename = param;
        } else {
            filename = param['filename'];
            rows = param['rows'];
            worksheet = param['worksheet'] || 'Worksheet';
        }
     
        var uri = 'data:application/vnd.ms-excel;base64,'
        , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body>{table}</body></html>'
        , base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
        , format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }

       
        var ctx = { worksheet: worksheet, table: table };
        var data = base64(format(template, ctx));
        if (window.navigator.msSaveBlob){
            var blob = b64toBlob(data);
            window.navigator.msSaveBlob(blob, filename);
        } else {
            var alink = $('<a style="display:none"></a>').appendTo('body');
            alink[0].href = uri + data;
            alink[0].download = filename;
            alink[0].click();
            alink.remove();
        }
    }

function b64toBlob(data){
        var sliceSize = 512;
        var chars = atob(data);
        var byteArrays = [];
        for(var offset=0; offset<chars.length; offset+=sliceSize){
            var slice = chars.slice(offset, offset+sliceSize);
            var byteNumbers = new Array(slice.length);
            for(var i=0; i<slice.length; i++){
                byteNumbers[i] = slice.charCodeAt(i);
            }
            var byteArray = new Uint8Array(byteNumbers);
            byteArrays.push(byteArray);
        }
        return new Blob(byteArrays, {
            type: ''
        });
 }

